GM.gm_lasercolor = {}
GM.lastdamage = {}
GM.svar = {}
GM.lastcolor = CurTime()
GM.lastarmor = CurTime()

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("helditem.lua")
AddCSLuaFile("cl_inventory.lua")
AddCSLuaFile("cl_lootventory.lua")
AddCSLuaFile("cl_targetid.lua")
AddCSLuaFile("cl_circle.lua")
AddCSLuaFile("cl_ghost.lua")
AddCSLuaFile("cl_help.lua")
AddCSLuaFile("shared.lua")
AddCSLuaFile("buildings.lua")
AddCSLuaFile("itemparser.lua")
AddCSLuaFile("recipes.lua")
//AddCSLuaFile("svar.lua")

include("config.lua")
include("shared.lua")
//include("svar.lua")
include("buildings.lua")
include("itemparser.lua")
include("recipes.lua")
include("doodads.lua")
include("rounds.lua")
include("maps.lua")

function GM:Initialized()
	self:ParseBuildings()
	self:ParseRecipes()
	//self:RegisterSvar( "kill_time", 120 )
	self.svar["ws_kill_time"] = 120
	
	resource.AddFile("models/mixerman3d/other/arrow.mdl")
	resource.AddFile("models/mixerman3d/other/arrow.dx80.vtx")
	resource.AddFile("models/mixerman3d/other/arrow.dx90.vtx")
	resource.AddFile("models/mixerman3d/other/arrow.phy")
	resource.AddFile("models/mixerman3d/other/arrow.sw.vtx")
	resource.AddFile("models/mixerman3d/other/arrow.vvd")
	
	resource.AddFile("materials/mixerman3d/other/metal_galv2.vmt")
	resource.AddFile("materials/mixerman3d/other/metal_galv2.vtf")
	resource.AddFile("materials/mixerman3d/other/metal_galv.vmt")
	resource.AddFile("materials/mixerman3d/other/metal_galv.vtf")
	resource.AddFile("materials/mixerman3d/other/skate_deck.vmt")
	resource.AddFile("materials/mixerman3d/other/skate_deck.vtf")
	resource.AddFile("materials/mixerman3d/other/skate_misc.vmt")
	resource.AddFile("materials/mixerman3d/other/skate_misc.vtf")
	
	resource.AddFile("materials/wintersurvival/icon_1vine.vmt")
	resource.AddFile("materials/wintersurvival/icon_1vine.vtf")
	resource.AddFile("materials/wintersurvival/icon_arrow.vmt")
	resource.AddFile("materials/wintersurvival/icon_arrow.vtf")
	resource.AddFile("materials/wintersurvival/icon_axe.vmt")
	resource.AddFile("materials/wintersurvival/icon_axe.vtf")
	resource.AddFile("materials/wintersurvival/icon_beer.vmt")
	resource.AddFile("materials/wintersurvival/icon_beer.vtf")
	resource.AddFile("materials/wintersurvival/icon_bow.vmt")
	resource.AddFile("materials/wintersurvival/icon_bow.vtf")
	resource.AddFile("materials/wintersurvival/icon_bowloaded.vmt")
	resource.AddFile("materials/wintersurvival/icon_bowloaded.vtf")
	resource.AddFile("materials/wintersurvival/icon_bugbait.vmt")
	resource.AddFile("materials/wintersurvival/icon_bugbait.vtf")
	resource.AddFile("materials/wintersurvival/icon_campfire.vmt")
	resource.AddFile("materials/wintersurvival/icon_campfire.vtf")
	resource.AddFile("materials/wintersurvival/icon_crystal.vmt")
	resource.AddFile("materials/wintersurvival/icon_crystal.vtf")
	resource.AddFile("materials/wintersurvival/icon_fence.vmt")
	resource.AddFile("materials/wintersurvival/icon_fence.vtf")
	resource.AddFile("materials/wintersurvival/icon_healthkit.vmt")
	resource.AddFile("materials/wintersurvival/icon_healthkit.vtf")
	resource.AddFile("materials/wintersurvival/icon_healthvial.vmt")
	resource.AddFile("materials/wintersurvival/icon_healthvial.vtf")
	resource.AddFile("materials/wintersurvival/icon_molotov.vmt")
	resource.AddFile("materials/wintersurvival/icon_molotov.vtf")
	resource.AddFile("materials/wintersurvival/icon_newspaper.vmt")
	resource.AddFile("materials/wintersurvival/icon_newspaper.vtf")
	resource.AddFile("materials/wintersurvival/icon_plank.vmt")
	resource.AddFile("materials/wintersurvival/icon_plank.vtf")
	resource.AddFile("materials/wintersurvival/icon_rock.vmt")
	resource.AddFile("materials/wintersurvival/icon_rock.vtf")
	resource.AddFile("materials/wintersurvival/icon_sap.vmt")
	resource.AddFile("materials/wintersurvival/icon_sap.vtf")
	resource.AddFile("materials/wintersurvival/icon_spine.vmt")
	resource.AddFile("materials/wintersurvival/icon_spine.vtf")
	resource.AddFile("materials/wintersurvival/icon_wood.vmt")
	resource.AddFile("materials/wintersurvival/icon_wood.vtf")
	
	resource.AddFile("sound/itemequip.wav")
	resource.AddFile("sound/itemopen.wav")
end

hook.Add("InitPostEntity","wsinit",function()
	local self = gmod.GetGamemode()
	
	self:NewRound()
end)

GM.LastThink = CurTime()
function GM:Think()
	local delta = CurTime() - self.LastThink
	self.LastThink = CurTime()
	
	if self:IsRoundStarted() then
		self:DoDecay(delta)
	end
	for k,v in pairs(player.GetAll()) do
		if not v:WSIsPigeon() then
			local w = v:WaterLevel()
			if w then
				local wd = w * delta * self.water.pain
				v:TakeDamage( wd )
				if w == 1 then //head is submerged
					//oxygen shit
				end
			end
		end
	end
end

function GM:PlayerShouldTakeDamage(victim,attacker)
	return true
end

function GM:PlayerInitialSpawn(pl)
	local uid = pl:UniqueID()
	self:iBdata(pl)
	
	self.pldat[uid] = {
		inventory = {},
		invsel = nil,
	}
end

function GM:PlayerSpawn(pl)
	local uid = pl:UniqueID()
	self.pldat[uid].inventory = {}
	
	pl:SetPos(self:PlayerSelectSpawn(pl):GetPos())
	
	//hook.Call("PlayerSetModel",GAMEMODE,pl)
	
	local models = {
		"models/player/Group03/female_01.mdl",
		"models/player/Group03/female_02.mdl",
		"models/player/Group03/female_03.mdl",
		"models/player/Group03/female_04.mdl",
		"models/player/Group03/male_01.mdl",
		"models/player/Group03/male_02.mdl",
		"models/player/Group03/male_03.mdl",
		"models/player/Group03/male_04.mdl",
		"models/player/Group03/male_05.mdl",
		"models/player/Group03/male_06.mdl",
		"models/player/Group03/male_07.mdl",
		"models/player/Group03/male_08.mdl",
		"models/player/Group03/male_09.mdl",
	}
	
	pl:SetModel(Model(models[math.random(#models)]))
	
	self:resetNet(pl)
	self:resetNeeds(pl)
	self:updateInventory(pl)
	self:updateTimer(pl)
	
	self:RoundPlayerSpawn(pl)
end

GM.SpawnPoints = {}function GM:PlayerSelectSpawn(pl)
	if not (type(self.SpawnPoints) == "table" and table.Count(self.SpawnPoints) > 1) then
		self.SpawnPoints = {}
		for k,v in pairs{
			"info_player_deathmatch",
			"info_player_combine",
			"info_player_rebel",
			"info_player_counterterrorist",
			"info_player_terrorist",
			"info_player_axis",
			"info_player_allies",
			"gmod_player_start",
			"info_player_teamspawn",
			"info_player_start",
		} do
			self.SpawnPoints = table.Add(self.SpawnPoints,ents.FindByClass(v))
		end
	end
	
	local select
	for i = 1,100 do
		select = self.SpawnPoints[math.random(1,table.Count(self.SpawnPoints))]
		
		local good = true
		for k,v in pairs(player.GetAll()) do
			if (select:GetPos() - v:GetPos()):Length() < 64 then
				good = false
			end
		end
		
		if good then
			break
		end
	end
	
	return select
end

function GM:DoPlayerDeath(pl,inf,killer)
	if not pl then
		return
	end
	local self = gmod.GetGamemode()
	local needstab = self.needs[pl]
	local uid = pl:UniqueID()
	
	if self:IsRoundStarted() and self.net[pl] and needstab and pl:Alive() and not pl:WSIsPigeon() then //Duplicity shared doDecay
	
		local fail = false
		for k,v in pairs(self.buildingdata) do
			if v.Owner == pl and v.class == "corpse" then //WARNING: This code means the game can work ONLY AS INTENDED. One player spawn per round, otherwise they are invincible.
				fail = true
			end
		end
		
		if not fail then
			self:resetNeeds(pl)
			
			wsUID = wsUID + 1
			
			//Create the lootable corpse here
			local corpse = ents.Create("ws_corpse")
			corpse:SetModel("models/props_c17/gravestone001a.mdl")
			corpse:SetPos(pl:GetPos())
			corpse:SetAngles(Angle(0,pl:EyeAngles().y,0))
			//corpse:SetOwner(pl)
			corpse:Spawn()
			//corpse:SetVelocity(pl:GetVelocity())
			
			self.buildingdata[wsUID] = {
				wsUID = wsUID,
				ents = {
					corpse,
				},
				class = "corpse",
				Owner = pl,
			}
			local tab = self.buildingdata[wsUID]
			tab.inventory = {}
			
			corpse:GetTable().wsUID = wsUID
			corpse:GetTable().class = "corpse"
			corpse:GetTable().Owner = pl
			
			//Remove weapon model
			/*
			local rp = RecipientFilter()
			rp:AddAllPlayers()
			
			umsg.Start("wsWeapon",rp)
				umsg.Entity(pl)
				umsg.String("!none") //Why the hell was this self.class?
			umsg.End()
			*/
			
			local class = ""
			local dat = self.pldat[uid]
			if dat then
				class = self.pldat[uid].invsel
			end
			if dat then
				dat = self.items[class]
			end
			if dat and dat.quantity >= 0 then
				local a = dat
				
				if a then
					a = a.onInvUnSelect
					if a then
						local b,err = pcall(a,dat,pl) //ITEM:onInvUnSelect(pl)
						if not b then
							Error("\n",err)
						end
					end
				end
			end
			//End
			
			//Transfer inventory
			local uid = pl:UniqueID()
			local inv = self.pldat[uid] and self.pldat[uid].inventory or nil
			if inv then
				pl:WSGive("meat",math.random(6,12))
				tab.inventory = inv
				self.pldat[uid].inventory = {}
			end
			
			pl:AddDeaths(1)
			//pl:CreateRagdoll()
			//End
		end
		
		//Force them into pigeon so AFKs don't stop server
		timer.Simple(3, function() pl.WSPigeon(pl) end)
	end
end

function GM:EntityTakeDamage(ent,inflictor,attacker,amount,dmginfo)
	//Msg(ent,"::",inflictor,"::",attacker,"::",amount,"\n")
	if inflictor:GetClass() == "ws_item" then
		local class = inflictor.class
		if class then
			local dat = self.items[class]
			if dat then
				maxdmg = dat.damage2 or dat.damage
				if maxdmg then
					dmginfo:SetDamage(math.min(dmginfo:GetDamage(),maxdmg))
				end
			end
		end
	end
end

//if SERVER then
	FindMetaTable("Player").WSGive = function(self,class,qty) //Accepts negative quantities
		local absqty = nil
		local qty = qty or 1
		local GM = gmod.GetGamemode()
		local uid = self:UniqueID()
		
		if GM.items[class] and GM.pldat[uid] then
			if GM.pldat[uid].inventory[class] then
				local dat = GM.pldat[uid].inventory[class]
				dat.quantity = dat.quantity + qty
				absqty = dat.quantity
			else
				GM.pldat[uid].inventory[class] = {
					item = GM.items[class],
					quantity = qty,
				}
				absqty = qty
			end
			local dat = GM.pldat[uid].inventory[class]
			if dat.quantity <= 0 then
				//table.remove(GM.pldat[uid].inventory,class) //Bugged
				
				//If equipped, use onInvUnSelect
				local a = dat
				
				if a and a.item.class == GM.pldat[uid].invsel then
					a = a.item.onInvUnSelect
					if a then
						local b,err = pcall(a,dat.item,self) //ITEM:onInvUnSelect(pl)
						if not b then
							Error("\n",err)
						end
					end
				end
				
				
				//Remove it from inventory
				GM.pldat[uid].inventory[class] = nil
				absqty = 0
			end
		end
		
		if absqty then
			//TODO:Make it send delta, not absolute
			GM:updateInventory(self,{
				[class] = {
					item = GM.items[class],
					quantity = absqty,
				},
			})
			
			if absqty <= 0 and class == GM.pldat[uid].invsel then
				GM.selectInventory(self,"wssel",{"!none"}) //Deselect item
			end
		else
			GM:updateInventory(self)
		end
	end
	
	FindMetaTable("Player").WSHas = function(self,class,qty)
		local qty = qty or 1
		local GM = gmod.GetGamemode()
		local uid = self:UniqueID()
		
		if GM.items[class] and GM.pldat[uid] then
			if GM.pldat[uid].inventory[class] then
				local dat = GM.pldat[uid].inventory[class]
				if dat.quantity >= qty then
					return true
				end
			end
		end
		return false
	end
	
	FindMetaTable("Player").WSBuff = function(self,type,percent) //Accepts negative percents
		local GM = gmod.GetGamemode()
		local uid = self:UniqueID()
		local needs = GM.needs[self]
		
		if needs and _G.type(needs[type]) == "number" then
			//Msg("Buffed ",type, " (",percent,") percent.\n\t(From ",needs[type]," percent to ")
			needs[type] = math.Clamp(needs[type] + percent,0,100)
			//Msg(needs[type]," percent!\n")
		end
		GM:updateNeeds(self)
	end
//end

function GM:PlayerLoadout(pl)
	//pl:Give("laser_smg")
	pl:Give("ws_empty")
	/*
	pl:WSGive("wood",4)
	pl:WSGive("crystal",1)
	pl:WSGive("rock",1)
	pl:WSGive("campfire",1)
	pl:WSGive("barrel",3)
	*/
end

--'Player hurt
hook.Add("PlayerHurt","wsPlayerHurt",function(pl,attacker,health,damage)
	local self = gmod.GetGamemode()
	
	if self.RoundTime + self.svar["ws_kill_time"] > CurTime() or (not IsValid(attacker) or attacker:GetClass() ~= "player") then
		local needstab = self.needs[pl]
		if not needstab then
			return
		end
		
		needstab.constitution = math.max(0,needstab.constitution - damage)
		pl:SetHealth(100)
		
		self:updateNeeds(pl)
	end
end)

--'
// Custom
--'

//Player tables
GM.needs = {}
GM.net = {}
GM.pldat = {}

//Networking

//Inventory
function GM:updateInventory(pl,inv)
	if not IsValid(pl) then
		return
	end
	local rel = inv and true or false
	local uid = pl:UniqueID()
	local inv = inv or self.pldat[uid].inventory
	
	//Key: class
	//Value: {item =,quantity =,}
	
	if inv then
		umsg.Start("wsInv",pl)
			umsg.Bool(rel)
			umsg.Short(table.Count(inv))
			for k,v in pairs(inv) do
				umsg.String(k)
				umsg.Short(v.quantity)
			end
		umsg.End()
	end
end

function GM.selectInventory(pl,cmd,args)
	if not IsValid(pl) then
		return
	end
	local self = gmod.GetGamemode()
	local uid = pl:UniqueID()
	local inv = self.pldat[uid].inventory
	local sel = args[1]
	if sel then
		sel = tostring(sel)
		
		local next = self.pldat[uid].invsel
		
		if sel == "!none" then
			next = nil
		elseif self.items[sel] and inv[sel] then
			next = sel
		end
		
		if next ~= self.pldat[uid].invsel then
			local prev = self.pldat[uid].invsel
			
			//Msg("Changed from ",prev," to ",next,"\n")
			
			if prev then
				local a
				a = inv[prev]
				if a then
					a = a.item.onInvUnSelect
					if a then
						local b,err = pcall(a,inv[prev].item,pl) //ITEM:onInvUnSelect(pl)
						if not b then
							Error("\n",err)
						end
					end
				end
			end
			
			a = inv[next]
			if a then
				a = a.item.onInvSelect
				if a then
					local b,err = pcall(a,inv[next].item,pl) //ITEM:onInvSelect(pl)
					if not b then
						Error("\n",err)
					end
				end
			end
		end
		
		umsg.Start("wsSel",pl)
			umsg.String(next or "!none")
		umsg.End()
		self.pldat[uid].invsel = next
	end
end
concommand.Add("wssel",GM.selectInventory)

function GM.dropInventory(pl,cmd,args)
	if not IsValid(pl) then
		return
	end
	if pl:WSIsPigeon() or not pl:Alive() then
		return
	end
	
	local self = gmod.GetGamemode()
	local uid = pl:UniqueID()
	local inv
	local id = tonumber(args[3])
	
	if (id == "-1") or not id then
		inv = self.pldat[uid].inventory
	elseif id > 0 then
		inv = self.buildingdata[id]
		
		if inv then
			oent = inv.ents[1]
			if (not IsValid(oent)) or (oent:GetPos() - pl:GetPos()):Length() > 256 then
				umsg.Start("wsBarOOR",pl)
					umsg.Long(itemFrom)
				umsg.End()
				return
			end
			
			inv = inv.inventory
		end
	else
		return
	end
	
	local itemClass = args[1]
	local itemCount = args[2]
	
	local item = inv[itemClass]
	
	if item and item.quantity > 0 and item.item.model then
		itemCount = math.min(item.quantity,itemCount)
		
		local sPos = nil
		local sAng = pl:EyeAngles()
		sAng:RotateAroundAxis(pl:EyeAngles():Right(),90)
		sAng:RotateAroundAxis(pl:EyeAngles():Up(),90)
		
		local tr = util.TraceLine{
			start = pl:GetShootPos(),
			endpos = pl:GetShootPos() + pl:GetAimVector()*72, //6 feet away
			filter = pl,
		}
		if tr.Hit then
			tr = util.TraceLine{
				start = pl:GetShootPos(),
				endpos = pl:GetShootPos() + Vector(0,0,1)*72, //6 feet away
				filter = pl,
			}
			sPos = pl:GetShootPos() + Vector(0,0,1)*math.min(40,tr.Fraction*72)
		else
			sPos = pl:GetShootPos() + pl:GetAimVector()*math.min(40,tr.Fraction*72)
		end
		
		for i = 1,itemCount do
			local drop = ents.Create("ws_item")
			drop:SetModel(item.item.model)
			drop:SetPos(sPos)
			drop:SetAngles(sAng)
			drop:GetTable().class = itemClass
			drop:Spawn()
			local phys = drop:GetPhysicsObject()
			phys:ApplyForceCenter(Vector(math.random()*500-250,math.random()*500-250,math.random()*500-250))
			
			if (id == "-1") or not id then
				pl:WSGive(itemClass,-1)
			else
				self:BuildingGive(id,itemClass,-1)
			end
		end
	end
end
concommand.Add("wsdrop",GM.dropInventory)

function GM.useInventory(pl,cmd,args)
	if not IsValid(pl) then
		return
	end
	if pl:WSIsPigeon() or not pl:Alive() then
		return
	end
	
	//Msg("USE ATTEMPT OF ")
	
	local self = gmod.GetGamemode()
	local uid = pl:UniqueID()
	local inv = self.pldat[uid].inventory
	local itemClass = args[1]
	local item = inv[itemClass].item
	
	//Msg(args[1],"\n")
	//PrintTable(item)
	
	if item and pl:WSHas(itemClass) and item:onInvUsable() then
		//Msg("Using...\n")
		local b,err = pcall(item.onInvUse,item,pl)
		if not b then
			error("\n"..tostring(err))
		end
	end
end
concommand.Add("wsuse",GM.useInventory)

function GM.moveInventory(pl,cmd,args)
	if not IsValid(pl) then
		return
	end
	if pl:WSIsPigeon() or not pl:Alive() then
		return
	end
	
	for i = 1,4 do
		if type(args[i]) ~= "string" then
			return
		end
	end
	
	local self = gmod.GetGamemode()
	local itemClass = args[1]
	local itemQuantity = tonumber(args[4])
	local itemFrom = tonumber(args[2])
	local itemTo = tonumber(args[3])
	
	if not (itemQuantity and itemFrom and itemTo and self.items[itemClass]) then
		return
	end
	if not ((itemFrom == -1 or self.buildingdata[itemFrom]) and (itemTo == -1 or self.buildingdata[itemTo])) then
		return
	end
	
	//Check distances
	if itemFrom ~= -1 then
		local a = self.buildingdata[itemFrom].ents[1]
		if IsValid(a) and (a:GetPos()-pl:GetPos()):Length() > 256 then
			umsg.Start("wsBarOOR",pl)
				umsg.Long(itemFrom)
			umsg.End()
			return
		end
	end
	if itemTo ~= -1 then
		local a = self.buildingdata[itemTo].ents[1]
		if IsValid(a) and (a:GetPos()-pl:GetPos()):Length() > 256 then
			umsg.Start("wsBarOOR",pl)
				umsg.Long(itemTo)
			umsg.End()
			return
		end
	end
	
	//Check quantities
	if not ((itemFrom == -1 and pl:WSHas(itemClass,itemQuantity)) or (itemFrom ~= -1 and self:BuildingHas(itemFrom,itemClass,itemQuantity))) then
		return
	end
	
	//Execute the move
	if itemFrom == -1 then
		pl:WSGive(itemClass,-1*itemQuantity)
	else
		self:BuildingGive(itemFrom,itemClass,-1*itemQuantity)
	end
	
	if itemTo == -1 then
		pl:WSGive(itemClass,itemQuantity)
	else
		self:BuildingGive(itemTo,itemClass,itemQuantity)
	end
end
concommand.Add("wsmove",GM.moveInventory)
//Else

function GM:resetNet(pl)
	self.net[pl] = {
		false,
	}
end

function GM:sendNet(pl)
	if self.net[pl] then
		umsg.Start("wsNet",pl)
			for k,v in pairs(self.net[pl]) do
				umsg.Bool(v)
			end
		umsg.End()
	end
end

function GM:resetNeeds(pl)
	self.needs[pl] = {
		cold = 0,
		thirst = 0,
		hunger = 0,
		fatigue = 0,
		constitution = 100,
	}
	self:updateNeeds(pl)
end

function GM:updateNeeds(pl)
	local self = gmod.GetGamemode()
	if self.needs[pl] then
		local dat = self.needs[pl]
		umsg.Start("wsNeeds",pl)
			umsg.Float(dat.cold)
			umsg.Float(dat.thirst)
			umsg.Float(dat.hunger)
			umsg.Float(dat.fatigue)
			umsg.Float(dat.constitution)
		umsg.End()
	end
end

function GM:iBdata(pl)
	local self = gmod.GetGamemode()
	local rp = RecipientFilter()
	rp:AddPlayer(pl)
	//for k,v in pairs(file.Find("ws/buildings/*.txt", "DATA")) do
	for k,v in pairs(file.Find("wintersurvival/content/data/ws/buildings/*.txt", LUA_PATH)) do
		//local str = file.Read("ws/buildings/"..v)
		local str = file.Read("gamemodes/wintersurvival/content/data/ws/buildings/".. v, true)
		
		local send = {}
		for i = 1,100 do
			if str:len() > 0 then
				table.insert(send,str:sub(1,200))
				str = str:sub(201)
			end
		end
		
		local class = v:sub(1,#v-4)
		
		for k,v in pairs(send) do
			umsg.Start("iBdata")
				umsg.String(class)
				//Msg("Sent >",class,"< (",k," of ",#send,")\n")
				//Msg("\t",v,"\n")
				umsg.Short(k)
				umsg.Short(#send)
				umsg.String(v)
				umsg.Bool((#send == k) and true or false)
			umsg.End()
		end
	end
	umsg.Start("iBdataFin")
	umsg.End()
end

function GM:crouching(pl)
	if pl:Alive() and pl:Crouching() then
		local g = pl:GetGroundEntity()
		if g:IsWorld() or g:IsValid() then
			return true
		end
	end
	return false
end

//Drinking


//Help, team, spares

function GM:ShowHelp(pl)
	umsg.Start("wsopt",pl)
		umsg.Short(1)
	umsg.End()
end

function GM:ShowTeam(pl)
	umsg.Start("wsopt",pl)
		umsg.Short(2)
	umsg.End()
end

function GM:ShowSpare1(pl)
	umsg.Start("wsopt",pl)
		umsg.Short(3)
	umsg.End()
end

function GM:ShowSpare2(pl)
	umsg.Start("wsopt",pl)
		umsg.Short(4)
	umsg.End()
end


//Helper

function GM:CreateItem(class)
	local item = self.items[class]
	if item and item.model then
		local drop = ents.Create("ws_item")
		drop:SetModel(item.model)
		drop:GetTable().class = class
		
		return drop
	end
	return nil
end


/*
local AnimTranslateTable = {}
AnimTranslateTable[PLAYER_RELOAD]		= ACT_HL2MP_GESTURE_RELOAD
AnimTranslateTable[PLAYER_JUMP]		= ACT_HL2MP_JUMP
AnimTranslateTable[PLAYER_ATTACK1]		= ACT_HL2MP_GESTURE_RANGE_ATTACK

function GM:SetPlayerAnimation(pl,anim)
	local act = ACT_HL2MP_IDLE
	//local act = ACT_HL2MP_GESTURE_RANGE_ATTACK_FIST
	local Speed = pl:GetVelocity():Length()
	local OnGround = pl:OnGround()
	
	//If it's in the translate table then just straight translate it
	if (AnimTranslateTable[ anim ] != nil) then
		act = AnimTranslateTable[ anim ]
	else
		//Crawling on the ground
		if (OnGround && pl:Crouching()) then
			act = ACT_HL2MP_IDLE_CROUCH
			
			if (Speed > 0) then
				act = ACT_HL2MP_WALK_CROUCH
			end
		elseif (Speed > 210) then
			//Player is running on ground
			act = ACT_HL2MP_RUN
		elseif (Speed > 0) then
			act = ACT_HL2MP_WALK
		end
	end
	
	//Attacking/Reloading is handled by the RestartGesture function
	if (act == ACT_HL2MP_GESTURE_RANGE_ATTACK || 
		act == ACT_HL2MP_GESTURE_RELOAD)
	then
		
		pl:RestartGesture(pl:Weapon_TranslateActivity(act))
		
		//If this was an attack send the anim to the weapon model
		if (act == ACT_HL2MP_GESTURE_RANGE_ATTACK) then
			pl:Weapon_SetActivity(pl:Weapon_TranslateActivity(ACT_RANGE_ATTACK1),0);
			
		end
		return
	end
	
	//Always play the jump anim if we're in the air
	if (!OnGround) then
		act = ACT_HL2MP_JUMP
	end
	
	//Ask the weapon to translate the animation and get the sequence
	//(ACT_HL2MP_JUMP becomes ACT_HL2MP_JUMP_AR2 for example)
	local seq = pl:SelectWeightedSequence(pl:Weapon_TranslateActivity(act))
	
	//If we're in a vehicle just sit down
	//We should let the vehicle decide this when we have scripted vehicles
	if (pl:InVehicle()) then
		//TODO! Different ACTS for different vehicles!
		local pVehicle = pl:GetVehicle()
		
		if (pVehicle.HandleAnimation != nil) then
		
			seq = pVehicle:HandleAnimation(pl)
			if (seq == nil) then return end
			
		else
			local class = pVehicle:GetClass()
			
			//
			//To realise why you don't need to add to this list,
			//See how the chair handles this (HandleAnimation)
			//
			if (class == "prop_vehicle_jeep") then
				seq = pl:LookupSequence("drive_jeep")
			elseif (class == "prop_vehicle_airboat") then
				seq = pl:LookupSequence("drive_airboat")
			else 
				seq = pl:LookupSequence("drive_pd")
			end
		end
	end
	
	
	//If the weapon didn't return a translated sequence just set 
	//the activity directly.
	if (seq == -1) then 
	
		//Hack.. If we don't have a weapon and we're jumping we
		//use the SLAM animation (prevents the reference anim from showing)
		if (act == ACT_HL2MP_JUMP) then
			act = ACT_HL2MP_JUMP_SLAM
		end
		
		seq = pl:SelectWeightedSequence(act)
	end
	
	//Don't keep switching sequences if we're already playing the one we want.
	if (pl:GetSequence() == seq) then return end
	
	//Set and reset the sequence
	pl:SetPlaybackRate(1.0)
	pl:ResetSequence(seq)
	pl:SetCycle(0)
end
*/











